package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.controller.BaseController;
import com.sky.dto.AddressBootDTO;
import com.sky.entity.AddressBootEntity;
import com.sky.result.Result;
import com.sky.service.AddressBootService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址谱相关接口")
@Slf4j
public class AddressBootController extends BaseController {

    @Autowired
    private AddressBootService addressBootService;

    @PostMapping
    @ApiOperation("新增地址普信息")
    public Result add(@RequestBody AddressBootDTO dto) {
        log.info("地址普新增信息=>{}", dto);
        addressBootService.add(dto);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询当前用户下所有的地址信息")
    public Result list() {
        log.info("查询当前用下所有的地址信息");
        List<AddressBootEntity> list = addressBootService.list();
        return Result.success(list);
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result getDefault() {
        log.info("查询默认地址");
        AddressBootEntity entity = addressBootService.getDefault();
        return Result.success(entity);
    }

    @PutMapping
    @ApiOperation("根据id修改信息")
    public Result update(@RequestBody AddressBootDTO dto) {
        log.info("修改地址信息=>{}", dto);
        addressBootService.update(dto);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除信息")
    public Result delete(Long id) {
        log.info("根据id删除信息 =>{}", id);
        addressBootService.delete(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询信息")
    public Result getById(@PathVariable Long id) {
        AddressBootEntity entity = addressBootService.getById(id);
        return Result.success(entity);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBootDTO dto) {
        log.info("设置默认地址=>{}", dto);
        addressBootService.setDefault(dto);
        return Result.success();
    }
}
